﻿using System;
using System.Collections.Generic;
using System.Data;
using Pku.BookStore.Model;
using System.Data.Common;

namespace Pku.BookStore.DAL
{
    /// <summary>
    /// 数据访问类
    /// </summary>
    public class OrderFormDAL : BaseDAL
    {
        string commandText = string.Empty;

        /// <summary>
        /// 增加订单
        /// </summary>
        /// <param name="entity"></param>
        public int Add(OrderFormModel entity)
        {
            commandText = @"INSERT INTO OrderForm(UserID,OrderStatus,OrderData,Recipients,Address,Phone,TotalPrice)
VALUES(@UserID,@OrderStatus,getdate(),@Recipients,@Address,@Phone,@TotalPrice);SET  @OrderID=scope_identity();
";
            var command = DbMain.GetSqlStringCommand(commandText);

            DbMain.AddInParameter(command, "UserID", DbType.Int32, entity.UserID);
            DbMain.AddInParameter(command, "OrderStatus", DbType.Int32, entity.OrderStatus);
            DbMain.AddInParameter(command, "Recipients", DbType.String, entity.Recipients);
            DbMain.AddInParameter(command, "Address", DbType.String, entity.Address);
            DbMain.AddInParameter(command, "Phone", DbType.AnsiString, entity.Phone);
            DbMain.AddInParameter(command, "TotalPrice", DbType.Decimal, entity.TotalPrice);
            DbMain.AddOutParameter(command, "OrderID", DbType.Int32, 10);
            DbMain.ExecuteNonQuery(command);
            return Convert.ToInt32(DbMain.GetParameterValue(command, "OrderID"));

        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="entity"></param>
        public bool Modify(OrderFormModel entity)
        {
            commandText = @"UPDATE OrderForm SET OrderID=@OrderID,UserID=@UserID,OrderStatus=@OrderStatus,OrderData=@OrderData,Recipients=@Recipients,Address=@Address,Phone=@Phone,TotalPrice=@TotalPrice WHERE OrderID=@OrderID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, entity.OrderID);
            DbMain.AddInParameter(command, "UserID", DbType.Int32, entity.UserID);
            DbMain.AddInParameter(command, "OrderStatus", DbType.Int32, entity.OrderStatus);
            DbMain.AddInParameter(command, "OrderData", DbType.DateTime, entity.OrderData);
            DbMain.AddInParameter(command, "Recipients", DbType.String, entity.Recipients);
            DbMain.AddInParameter(command, "Address", DbType.String, entity.Address);
            DbMain.AddInParameter(command, "Phone", DbType.AnsiString, entity.Phone);
            DbMain.AddInParameter(command, "TotalPrice", DbType.Decimal, entity.TotalPrice);

            return (int)DbMain.ExecuteNonQuery(command) == 1;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public List<OrderFormModel> Search(OrderFormParam param)
        {
            var list = new List<OrderFormModel>();
            DbCommand dbCommand = DbMain.GetStoredProcCommand("OrderFormPageSearch");
            dbCommand.CommandTimeout = 600;
            DbMain.AddInParameter(dbCommand, "DateStart", DbType.String, param.BeginDate.ToString("yyyy-MM-dd 00:00:00"));
            DbMain.AddInParameter(dbCommand, "DateEnd", DbType.String, param.EndDate.ToString("yyyy-MM-dd 23:59:59"));
            DbMain.AddInParameter(dbCommand, "UserName", DbType.String, param.UserName);
            DbMain.AddInParameter(dbCommand, "OrderID", DbType.Int32, param.OrderID);
            DbMain.AddInParameter(dbCommand, "Page", DbType.Int32, param.PageNo);
            DbMain.AddInParameter(dbCommand, "Rows", DbType.Int32, param.PageRow);
            using (var dr = DbMain.ExecuteReader(dbCommand))
            {
                while (dr.Read())
                {
                    //list.Add(ConvertReaderToEntity(dr));
                    var m = ConvertReaderToEntity(dr);
                    m.UserName = dr["UserName"].ToString();
                    OrderDetailDAL dal = new OrderDetailDAL();
                    m.OrderList = dal.GetListById(m.OrderID);
                    list.Add(m);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int GetCount(Model.OrderFormParam param)
        {
            string commandText = "SELECT COUNT(1) FROM OrderForm o(NOLOCK) WHERE 1=1";
            var command = DbMain.GetSqlStringCommand(commandText);
            return (int)DbMain.ExecuteScalar(command);
        }

        /// <summary>
        /// 获取订单实体
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public OrderFormModel GetEntityById(int orderID)
        {
            commandText = @"SELECT OrderID,UserID,OrderStatus,OrderData,Recipients,Address,Phone,TotalPrice  FROM  OrderForm WHERE OrderID=@OrderID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, orderID);
            using (var dr = DbMain.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    var m = ConvertReaderToEntity(dr);
                    OrderDetailDAL dal = new OrderDetailDAL();
                    m.OrderList = dal.GetListById(m.OrderID);
                    return m;
                }
            }
            return null;
        }

        /// <summary>
        /// 转换实体
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static OrderFormModel ConvertReaderToEntity(IDataRecord dr)
        {
            return new OrderFormModel
            {
                OrderID = (int)dr["OrderID"],
                UserID = (int)dr["UserID"],
                OrderStatus = (int)dr["OrderStatus"],
                OrderData = Convert.ToDateTime(dr["OrderData"]),
                Recipients = dr["Recipients"].ToString(),
                Address = dr["Address"].ToString(),
                Phone = dr["Phone"].ToString(),
                TotalPrice = (decimal)dr["TotalPrice"]
            };
        }

        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="entity"></param>
        public bool ModifyStatus(OrderFormModel entity)
        {
            commandText = @"UPDATE OrderForm SET OrderStatus=@OrderStatus WHERE OrderID=@OrderID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, entity.OrderID);
            DbMain.AddInParameter(command, "OrderStatus", DbType.Int32, entity.OrderStatus);

            return (int)DbMain.ExecuteNonQuery(command) == 1;
        }

        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="entity"></param>
        public bool ModifyStatus(OrderFormModel entity, int UserID)
        {
            commandText = @"UPDATE OrderForm SET OrderStatus=@OrderStatus WHERE OrderID=@OrderID AND UserID=@UserID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "OrderID", DbType.Int32, entity.OrderID);
            DbMain.AddInParameter(command, "UserID", DbType.Int32, UserID);
            DbMain.AddInParameter(command, "OrderStatus", DbType.Int32, entity.OrderStatus);

            return (int)DbMain.ExecuteNonQuery(command) == 1;
        }
    }

}